package com.shsh.safemanage.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PostcheckMonthSanweiReport implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4437538478752158468L;

	private int year;
	private int month;
	private Long depart1ID;
	private String depart1Name;
	private Long problemID;

	private List<Problem> postcheckList;
	private Map<Long, Problem> departSanweiList;

	/**
	 * KEY = depatment1.ID, VALUE= DEPARTMENT1
	 */
	private Map<Long, Department1> shiyebuList;
	private Map<Long, Department1> chushiList;
	/**
	 * KEY = depatment1.ID, VALUE= Sanwei count
	 */
	private Map<Long, Integer> shiyebuTable;
	private Map<Long, Integer> chushiTable;

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public Long getDepart1ID() {
		return depart1ID;
	}

	public void setDepart1ID(Long depart1ID) {
		this.depart1ID = depart1ID;
	}

	public String getDepart1Name() {
		return depart1Name;
	}

	public void setDepart1Name(String depart1Name) {
		this.depart1Name = depart1Name;
	}

	public Long getProblemID() {
		return problemID;
	}

	public void setProblemID(Long problemID) {
		this.problemID = problemID;
	}

	public Map<Long, Problem> getDepartSanweiList() {
		if (departSanweiList == null) {
			departSanweiList = new LinkedHashMap<Long, Problem>();
		}
		return departSanweiList;
	}

	public List<Problem> getCorpPostcheckList() {
		List<Problem> all = getPostcheckList();
		Map<Long, Problem> tmpList = new HashMap<Long, Problem>(getDepartSanweiList());
		for (Problem p : all) {
			if (tmpList.containsKey(p.getWriterDepartment1ID())) {
				tmpList.remove(p.getWriterDepartment1ID());
			}
		}

		all.addAll(tmpList.values());

		Collections.sort(all, SanweiProblemComparator.getInstance());
		
		// Problem[] postcheckArray = all.toArray(new Problem[all.size()]);
		// Arrays.sort(postcheckArray, SanweiProblemComparator.getInstance());
		//
		// all = new ArrayList<Problem>();
		// all.addAll(Arrays.asList(postcheckArray));

		return all;
	}

	public List<Problem> getPostcheckList() {
		if (postcheckList == null || postcheckList.size() == 0 || depart1ID != null) {
			// Sanwei report of department is ordered by SQL
			return postcheckList;
		}
		
		Collections.sort(postcheckList, SanweiProblemComparator.getInstance());

		// Problem[] postcheckArray = postcheckList.toArray(new
		// Problem[postcheckList.size()]);
		// Arrays.sort(postcheckArray, SanweiProblemComparator.getInstance());
		//
		// postcheckList = new ArrayList<Problem>();
		// postcheckList.addAll(Arrays.asList(postcheckArray));

		return postcheckList;
	}

	public void setPostcheckList(List<Problem> postcheckList) {
		this.postcheckList = postcheckList;
	}

	public Map<Long, Integer> getShiyebuTable() {
		return shiyebuTable;
	}

	public void setShiyebuTable(Map<Long, Integer> shiyebuTable) {
		this.shiyebuTable = shiyebuTable;
	}

	public Map<Long, Integer> getChushiTable() {
		return chushiTable;
	}

	public void setChushiTable(Map<Long, Integer> chushiTable) {
		this.chushiTable = chushiTable;
	}

	public Map<Long, Department1> getShiyebuList() {
		return shiyebuList;
	}

	public void setShiyebuList(Map<Long, Department1> shiyebuList) {
		this.shiyebuList = shiyebuList;
	}

	public Map<Long, Department1> getChushiList() {
		return chushiList;
	}

	public void setChushiList(Map<Long, Department1> chushiList) {
		this.chushiList = chushiList;
	}

	public void setIsSanwei(Long problemID, boolean isSanwei) {
		for (Problem p : postcheckList) {
			if (p.getId().equals(problemID)) {
				p.setIsSanwei(isSanwei);
				return;
			}
		}
	}

}

final class SanweiProblemComparator implements Comparator<Problem> {
	private static SanweiProblemComparator instance;

	public static Comparator<Problem> getInstance() {
		if (instance == null) {
			instance = new SanweiProblemComparator();
		}
		return instance;
	}

	private SanweiProblemComparator() {

	}

	public int compare(Problem o1, Problem o2) {
		int compare = o1.getWriterDepartment1ID().intValue() - o2.getWriterDepartment1ID().intValue();
		if (compare == 0) {
			compare = o1.getPostcheckID().intValue() - o2.getPostcheckID().intValue();
		}
		if (compare == 0) {
			compare = o1.getDepartment1ID().intValue() - o2.getDepartment1ID().intValue();
		}
		if (compare == 0) {
			compare = o1.getEquipmentID().intValue() - o2.getEquipmentID().intValue();
		}

		return compare;
	}
}
